﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WadViewer.DomainModel;
using System.Configuration;
using WadViewer.Configuration;

namespace WadViewer.ConsoleApp
{
	class Program
	{
		static void Main(string[] args)
		{
			WadType type;
			DateTime from, to;
			int max;
			string connectionName;
			if (CheckCommandLine(args, out type, out from, out to, out max, out connectionName))
			{
				var repository = new WadRepository();

				TableConnection connection = null;
				if (connectionName != null)
				{
					connection = ConnectionManager.GetConnections().First(c => c.Name == connectionName);
				}
				if(connection == null)
				{
					connection = ConnectionManager.GetConnections().First();
				}
				repository.Setup(connection);

				switch (type)
				{
					case WadType.TraceLogs:
						ShowTraceLogs(repository, from, to, max);
						break;
					case WadType.EventLogs:
						ShowEventLogs(repository, from, to, max);
						break;
					case WadType.PerfCounters:
						ShowPerfCounters(repository, from, to, max);
						break;
				}
			}
		}

		private static void ShowTraceLogs(WadRepository repository, DateTime from, DateTime to, int max)
		{
			var traceLogs = repository.RetrieveTraceLogs(from, to, max);
			foreach (var traceLog in traceLogs)
			{
				Console.WriteLine(traceLog);
			}
		}

		private static void ShowEventLogs(WadRepository repository, DateTime from, DateTime to, int max)
		{
			var eventLogs = repository.RetrieveEventLogs(from, to, max);
			foreach (var eventLog in eventLogs)
			{
				Console.WriteLine(eventLog);
			}
		}

		private static void ShowPerfCounters(WadRepository repository, DateTime from, DateTime to, int max)
		{
			var perfCounters = repository.RetrievePerfCounters(from, to, max);
			foreach (var perfCounter in perfCounters)
			{
				Console.WriteLine(perfCounter);
			}
		}

		private static bool CheckCommandLine(string[] args, out WadType type, out DateTime from, out DateTime to, out int max, out string connectionName)
		{
			if (args.Length < 4 || args.Length > 5)
			{
				Console.WriteLine("Wrong number of arguments");
				DisplayCommandLine();
				type = WadType.Unknown; from = DateTime.MinValue; to = DateTime.MinValue; max = 0;
				connectionName = null;
				return false;
			}

			switch (args[0])
			{
				case "TraceLogs":
				case "TraceLog":
					type = WadType.TraceLogs;
					break;
				case "EventLogs":
				case "EventLog":
					type = WadType.EventLogs;
					break;
				case "PerfCounters":
				case "PerfCounter":
					type = WadType.PerfCounters;
					break;
				default:
					type = WadType.Unknown;
					from = DateTime.MinValue;
					to = DateTime.MinValue;
					max = 0;
					connectionName = null;
					Console.WriteLine("Wrong [type]");
					DisplayCommandLine();
					return false;
			}

			if (!DateTime.TryParse(args[1], out from))
			{
				Console.WriteLine("Cannot parse [from]");
				DisplayCommandLine();
				to = DateTime.MinValue;
				max = 0;
				connectionName = null;
				return false;
			}

			if (!DateTime.TryParse(args[2], out to))
			{
				Console.WriteLine("Cannot parse [to]");
				DisplayCommandLine();
				max = 0;
				connectionName = null;
				return false;
			}

			if (!int.TryParse(args[3], out max))
			{
				Console.WriteLine("Cannot parse [max]");
				DisplayCommandLine();
				connectionName = null;
				return false;
			}

			if (max <= 0) max = 100;
			if (max > 50000) max = 50000;

			if (args.Length == 5)
			{
				connectionName = args[4];
			}
			else
			{
				connectionName = null;
			}

			return true;
		}

		private static void DisplayCommandLine()
		{
			Console.WriteLine("Usage: WadViewer [type] [from] [to] [max]");
			Console.WriteLine("[type]=TraceLogs | EventLogs | PerfCounters");
			Console.WriteLine("[max]= number from 100 to 50.000");
		}
	}
}
